package com.bawei.lib_common.base

import android.app.Activity
import android.app.Application
import android.content.res.Configuration
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.AppConfig
import com.bawei.lib_common.base.flow_eventbus.EventBusInitializer
import com.bawei.lib_net.storage.AppMMKV
import com.tencent.bugly.crashreport.CrashReport
import com.tencent.mmkv.MMKV
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.BuildConfig
import me.jessyan.autosize.onAdaptListener
import me.jessyan.autosize.utils.ScreenUtils


/**
 *作者：lxy
 *功能:
 *时间：2025/5/22
 */

open class BaseApplication : Application() {

    companion object {
        lateinit var instance: BaseApplication
    }

    override fun onCreate() {
        super.onCreate()
        instance = this
        initARouter()
        initMMKV()
        initAppHolder()
        AutoSize.initCompatMultiProcess(this) // 屏幕适配适配多进程
        initUmeng()
        initBugly()
    }

    private fun initBugly() {
        if (AppMMKV.splashAgreement) {
            CrashReport.initCrashReport(this, "9bbf7621a2", true);
        }
    }

    private fun initUmeng() {
        //主线程预初始化
        UMConfigure.preInit(this, "6849667279267e021080a6c5", "channel")
        // 自动采集选择:只能采集activity，而且全部activity
//        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
//        //手动采集选择：按需采集
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.MANUAL);
        //设置LOG开关，默认为false
        UMConfigure.setLogEnabled(true);
        //判断用户是否同意过:正式初始化，建议放在后台线程中，减少app启动时间
        if (AppMMKV.splashAgreement) {
            Thread {
                UMConfigure.init(
                    this,
                    "6849667279267e021080a6c5",
                    "channel",
                    UMConfigure.DEVICE_TYPE_PHONE,
                    null
                )
            }.start()

        }
    }


    private fun initAppHolder() {

    }

    private fun initMMKV() {
        MMKV.initialize(this)
    }

    private fun initARouter() {
        ARouter.openLog()
        ARouter.openDebug()
        ARouter.init(this)
        EventBusInitializer.init(this)
    }

    private fun initAutoSize() {
        AutoSizeConfig.getInstance()
            .setLog(BuildConfig.DEBUG)
            .setPrivateFontScale(1f)
            .onAdaptListener = object : onAdaptListener {
            override fun onAdaptBefore(target: Any?, activity: Activity?) {
                AutoSizeConfig.getInstance().screenWidth = ScreenUtils.getScreenSize(activity)[0]
                AutoSizeConfig.getInstance().screenHeight = ScreenUtils.getScreenSize(activity)[1]
                if (activity?.resources?.configuration?.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    AutoSizeConfig.getInstance().designHeightInDp = AppConfig.DESIGN_WIDTH_IN_DP
                    AutoSizeConfig.getInstance().designWidthInDp = AppConfig.DESIGN_HEIGHT_IN_DP
                } else {
                    AutoSizeConfig.getInstance().designHeightInDp = AppConfig.DESIGN_HEIGHT_IN_DP
                    AutoSizeConfig.getInstance().designWidthInDp = AppConfig.DESIGN_WIDTH_IN_DP
                }
            }

            override fun onAdaptAfter(target: Any?, activity: Activity?) {

            }
        }
    }
}


